package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@RequiredArgsConstructor
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement> implements LeaseAgreementService {

    private final LeaseAgreementMapper leaseAgreementMapper;

    private final ApartmentInfoMapper apartmentInfoMapper;

    private final RoomInfoMapper roomInfoMapper;

    private final GraphInfoMapper graphInfoMapper;

    private final LeaseTermMapper leaseTermMapper;

    private final PaymentTypeMapper paymentTypeMapper;

    @Override
    public List<AgreementItemVo> listAgreementItemVoByPhone(String phone) {
        List<AgreementItemVo> agreementItemVoList = leaseAgreementMapper.listAgreementItemVoByPhone(phone).stream().peek(agreementItemVo -> {
            LeaseStatus leaseStatus = agreementItemVo.getLeaseStatus();
            agreementItemVo.setIsRenewable(leaseStatus.equals(LeaseStatus.SIGNING) || leaseStatus.equals(LeaseStatus.RENEWING));
        }).collect(Collectors.toList());
        return agreementItemVoList;
    }

    // 写得稀烂，待改善
    @Override
    public AgreementDetailVo getAgreementDetailById(Long id) {
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();

        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if (null == leaseAgreement) {
            return null;
        }
        // apartment
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());

        // roomInfo
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());

        // apartmentGraph
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, apartmentInfo.getId()).eq(GraphInfo::getItemType, ItemType.APARTMENT));
        List<GraphVo> apartmentGraphVos = graphInfos.stream().map(graphInfo -> GraphVo.builder().name(graphInfo.getName()).url(graphInfo.getUrl()).build()).collect(Collectors.toList());

        // roomGraph
        List<GraphVo> roomGraphVos = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, roomInfo.getId()).eq(GraphInfo::getItemType, ItemType.ROOM)).stream()
                .map(graphInfo -> GraphVo.builder().name(graphInfo.getName()).url(graphInfo.getUrl()).build()).collect(Collectors.toList());

        // leaseTerm
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());
        if (null == leaseTerm) {
            agreementDetailVo.setLeaseTermMonthCount(null);
            agreementDetailVo.setLeaseTermUnit(null);
        } else {
            agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
            agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());
        }

        // paymentType
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());
        if (null == paymentType) {
            agreementDetailVo.setPaymentTypeName(null);
        } else {
            agreementDetailVo.setPaymentTypeName(paymentType.getName());
        }

        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphVos);
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        agreementDetailVo.setRoomGraphVoList(roomGraphVos);

        return agreementDetailVo;
    }
}




